from scrapy.spider import Spider
from scrapy.selector import Selector

from spider.items import SpiderItem


class CarSpider(Spider):
    name = 'car'
    start_urls = ["http://car.autohome.com.cn/zhaoche/pinpai/"]

    def parse(self,response):

        sel = Selector(response)
        areas = sel.xpath('//div[@class="grade_js_top30"]')
        items = [] 
        for area in areas:

            item = SpiderItem()

            item["brand"] = area.xpath('.//div[@class="grade_js_top33"]//a/text()').extract()
            item["brandurl"] = area.xpath('.//div[@class="grade_js_top33"]//a/@href').extract()
        
            item["carstyle"] = area.xpath('.//a[@style="text-decoration:none;"]/text()').extract()
            
            item["carstyleurl"] = area.xpath('.//a[@style="text-decoration:none;"]/@href').extract()
            item["type"] = "car"
            items.append(item)
        return items
